package com.jhgsys.internal.hazard.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.hazard.entity.HazardReportAreaUser;
import com.jhgsys.internal.hazard.mapper.HazardReportAreaUserMapper;
import com.jhgsys.internal.hazard.service.IHazardReportAreaUserService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author wanghw
 * @date 2024-09-18 14:27:18
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardReportAreaUserServiceImpl extends ServiceImpl<HazardReportAreaUserMapper, HazardReportAreaUser> implements IHazardReportAreaUserService {

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-09-18 14:27:18
     * @param request QueryRequest
     * @param hazardReportAreaUser hazardReportAreaUser
     * @return IPage<HazardReportAreaUser>
    */
    @Override
    public IPage<HazardReportAreaUser> findHazardReportAreaUserPage(QueryRequest request, HazardReportAreaUser hazardReportAreaUser) {
        LambdaQueryWrapper<HazardReportAreaUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportAreaUser::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardReportAreaUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-09-18 14:27:18
     * @param hazardReportAreaUser hazardReportAreaUser
     * @return List<HazardReportAreaUser>
     */
    @Override
    public List<HazardReportAreaUser> findHazardReportAreaUserList(HazardReportAreaUser hazardReportAreaUser) {
	    LambdaQueryWrapper<HazardReportAreaUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportAreaUser::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author wanghw
    * @date 2024-09-18 14:27:18
    * @param id
    * @return HazardReportAreaUser
    */
    @Override
    public HazardReportAreaUser findHazardReportAreaUserById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author wanghw
     * @date 2024-09-18 14:27:18
     * @param hazardReportAreaUser hazardReportAreaUser
     */
    @Override
    @Transactional
    public boolean createHazardReportAreaUser(HazardReportAreaUser hazardReportAreaUser) {
        return this.save(hazardReportAreaUser);
    }

    /**
     * 修改
     * @author wanghw
     * @date 2024-09-18 14:27:18
     * @param hazardReportAreaUser hazardReportAreaUser
     */
    @Override
    @Transactional
    public boolean updateHazardReportAreaUser(HazardReportAreaUser hazardReportAreaUser) {
        return this.updateById(hazardReportAreaUser);
    }

    /**
     * 逻辑删除
     * @author wanghw
     * @date 2024-09-18 14:27:18
     * @param hazardReportAreaUser hazardReportAreaUser
     * @param hazardReportAreaUserIds hazardReportAreaUserIds
     */
    @Override
    @Transactional
    public boolean deleteHazardReportAreaUser(HazardReportAreaUser hazardReportAreaUser,String hazardReportAreaUserIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(hazardReportAreaUserIds.split(StringPool.COMMA));

        hazardReportAreaUser.setDeletedBy(user.getUserName());
        hazardReportAreaUser.setDeletedTime(new Date());
        hazardReportAreaUser.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(hazardReportAreaUser,new LambdaQueryWrapper<HazardReportAreaUser>().in(HazardReportAreaUser::getReportAreaUserId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author wanghw
    * @date 2024-09-18 14:27:18
    * @param hazardReportAreaUserIds hazardReportAreaUserIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteHazardReportAreaUser(String hazardReportAreaUserIds) {
        List<String> list = Arrays.asList(hazardReportAreaUserIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardReportAreaUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardReportAreaUser::getReportAreaUserId,list);
        return this.remove(wapper);
    }


    /**
     * 逻辑删除
     * @author wanghw
     * @date 2024-09-18 14:27:18
     * @param hazardReportAreaUser hazardReportAreaUser
     * @param reportId reportId
     */
    @Override
    @Transactional
    public boolean deleteHazardReportAreaUserByReportId(HazardReportAreaUser hazardReportAreaUser,String reportId) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        hazardReportAreaUser.setDeletedBy(user.getUserName());
        hazardReportAreaUser.setDeletedTime(new Date());
        hazardReportAreaUser.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(hazardReportAreaUser,new LambdaQueryWrapper<HazardReportAreaUser>().in(HazardReportAreaUser::getReportId,reportId));
        if (update > 0 ){
            result = true;
        }
        return result;
    }
}
